#include "Frm08.h"

Frm08::Frm08(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm08::~Frm08() {
}

void Frm08::onInit() {

}

void Frm08::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 93, 97, false);
	wMMButton1->setImg("1", "./images/arrow_34_93X97.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_93X97.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Arc : wMArc1                         -----------------------------*/
	wMArc1 = new STTArc(p_rt, this, "wMArc1");
	wMArc1->setMainArg(412, 241, 200, 200, 100, 0, 100, 91, 450);
	wMArc1->setMainStyle(9999, 0xffffff, 0xffffff, 0, 0, 0, 0xffffff, 30, 0xa0a0a0, true, "");
	wMArc1->setIndicStyle(0x1a2b1, false, 30, "");
	wMArc1->setKnobStyle(false, 0, 0xffffff, 0xffffff, 0, 0x1a2b1, 0, 9999, "");
	wMArc1->bindData(_V("倒计时_秒"));

	/*----------------------------- Roller : wMRoller1                   -----------------------------*/
	wMRoller1 = new STTRoller(p_rt, this, "wMRoller1");
	wMRoller1->setMainArg(382, 26, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller1->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller1->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller1->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMRoller1->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm08::wMRoller1_vch_cb);

	/*----------------------------- Roller : wMRoller2                   -----------------------------*/
	wMRoller2 = new STTRoller(p_rt, this, "wMRoller2");
	wMRoller2->setMainArg(480, 26, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller2->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller2->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller2->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMRoller2->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm08::wMRoller2_vch_cb);

	/*----------------------------- Roller : wMRoller3                   -----------------------------*/
	wMRoller3 = new STTRoller(p_rt, this, "wMRoller3");
	wMRoller3->setMainArg(578, 26, 64, 150, 0, "选项1\n选项2\n选项3", 0, false);
	wMRoller3->setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 1, 0xc0c0c0, 0, 0x0);
	wMRoller3->setSelectedStyle(0, 0x1a2b1, 0xffffff, 0, 255);
	wMRoller3->setFontStyle(0x0, 16, "文泉驿微米黑", 0xffffff, 16, "文泉驿微米黑", 0, 0);
	wMRoller3->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm08::wMRoller3_vch_cb);

	/*----------------------------- Button : wMButton1                   -----------------------------*/
	wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
	wMButton1->setMainArg(413, 187, 198, 42, false);
	wMButton1->setPressedArg("开始倒计时", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->setReleasedArg("开始倒计时", 0x0, "文泉驿微米黑", 16, 0);
	wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton1->onEventHandler(Event::CLICKED, this, (EHandler) &Frm08::wMButton1_clk_cb);

	/*----------------------------- Label : wMLabel1                     -----------------------------*/
	wMLabel1 = new STTLabel(p_rt, this, "wMLabel1");
	wMLabel1->setMainArg(445, 84, 36, 33, "时", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel1->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel1->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel2                     -----------------------------*/
	wMLabel2 = new STTLabel(p_rt, this, "wMLabel2");
	wMLabel2->setMainArg(543, 84, 36, 33, "分", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel2->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel2->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(641, 84, 36, 33, "秒", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel3->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*----------------------------- Label : wMLabel4                     -----------------------------*/
	wMLabel4 = new STTLabel(p_rt, this, "wMLabel4");
	wMLabel4->setMainArg(412, 466, 200, 40, "", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel4->setFonts(0x0, 16, "文泉驿微米黑", 0);
	wMLabel4->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel4->setFormatDate("-", ":", false, false, false, true, true, true, false);
	wMLabel4->bindData(_V("倒计时_秒"));

	/*----------------------------- Timer : wMTimer1                     -----------------------------*/
	wMTimer1 = new STTTimer(p_rt, this, "wMTimer1", false);
	wMTimer1->set(1000, 0);
	wMTimer1->onEventHandler(Event::READY, this, (EHandler) &Frm08::wMTimer1_timer_cb);

	/*----------------------------- Button : wMButton2                   -----------------------------*/
	wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
	wMButton2->setMainArg(413, 513, 198, 42, false);
	wMButton2->setPressedArg("确认", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->setReleasedArg("确认", 0x0, "文泉驿微米黑", 16, 0);
	wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
	wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm08::wMButton2_clk_cb);
	wMButton2->opVisibleObj(_V("倒计时_秒"), R"({"c":"0"})");

	/*called after widget created.*/

	return; //createParts END
}

static string makeOption(int imin, int imax) {
	vector<string> vec;
	for(int i=imin;i<=imax;i++) {
		string tmp = "{\"id\":\"" + to_string(i) + "\",\"name\":\"" + to_string(i) + "\"}";
		vec.push_back(tmp);
	}
	string ret = "[" + StringUtil::join(vec, ",") + "]";
	return ret;
}

void Frm08::onLoad() {
	wMRoller1->setOptions(makeOption(0,23), 0);
	wMRoller2->setOptions(makeOption(0,59), 0);
	wMRoller3->setOptions(makeOption(0,59), 0);
}

bool Frm08::onClosing() {
	return true;
}

void Frm08::onDispose() {
	misc::WavePlayer::Pause();
}

void Frm08::wMRoller1_vch_cb(uint16_t code, LvEvent e) {
}

void Frm08::wMRoller2_vch_cb(uint16_t code, LvEvent e) {
}

void Frm08::wMRoller3_vch_cb(uint16_t code, LvEvent e) {
}

static int getSec(Frm08* frm) {
	int h = frm->wMRoller1->getSelectedIndex();
	int m = frm->wMRoller2->getSelectedIndex();
	int s = frm->wMRoller3->getSelectedIndex();
	int sec = h*3600+m*60+s;
	return sec;
}

void Frm08::wMButton1_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton1（开始倒计时）的点击事件*/
	int sec = getSec(this);
	wMArc1->setRange(0, sec);
	vm->setInt("倒计时_秒", sec);
	wMTimer1->start();
}

void playSound() {
#if __WIN32__
	string filename = ".";
#else
	string filename = "/customer/prm";
#endif
	filename += "/res/audio/firstblood.wav";
	misc::WavePlayer::playWave(filename, -1, 50);
}

void Frm08::wMTimer1_timer_cb(uint16_t code, LvEvent e) {
	int sec;
	vm->getInt("倒计时_秒", sec);
	if(sec == 0) {
		wMTimer1->stop();
		playSound();
	} else {
		vm->setInt("倒计时_秒", sec-1);
	}

}

void Frm08::wMButton2_clk_cb(uint16_t code, LvEvent e) {
	/*wMButton2（确认）的点击事件*/
	misc::WavePlayer::Pause();
}
